package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.databinding.ObservableField
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.DmApiClient
import com.cheyun.netsalev3.utils.api.DmBaseResp
import com.cheyun.netsalev3.utils.api.dm.DmApiCallback
import com.cheyun.netsalev3.utils.api.dm.DmApiException
import com.cheyun.netsalev3.view.*
import com.cheyun.netsalev3.view.dm.DmSwitchingDealersActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author Michael
 * @date  2018/12/19 - 16:03.
 */
class DmMyFragmentViewModel : BaseViewModel() {
    var name = ObservableField<String>("")
    var rname = ObservableField<String>("")

    init {
        bindData()
    }

    fun bindData(){
        var loginData = MySharedPreferences.getDmLoginData()
        loginData?.apply {
            var tabname = this?.distributorName
            name.set(tabname)
            var tmobile = this?.user?.phone
            if (!tmobile.isNullOrBlank() && tmobile.length > 7) {
                tmobile = tmobile.substring(0, 3) + "****" + tmobile.substring(tmobile.length - 4)
            }
            var trealname = this?.user?.nickname + "    " + tmobile
            rname.set(trealname)
        }
    }

    /**
     * 点击更多切换经销商
     */
    fun onClickDistributor(view: View) {
        view.context.startActivity(Intent(view.context, DmSwitchingDealersActivity::class.java))
    }

    /**
     * 客服中心
     */
    fun kefu(view: View) {
        view.context.startActivity(Intent(view.context, CustomerServiceCenterActivity::class.java))
    }

    fun onClickModule(view: View) {
        view.context.startActivity(Intent(view.context, ModuleActivity::class.java))
    }

    fun loginOut(view:View){
        DmApiClient.getInstance().kefuBusy(MySharedPreferences.getMobile(),"1")
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<Any, Any>>() {
                    override fun onSuccess(t: DmBaseResp<Any, Any>) {

                    }

                    override fun onError(ex: DmApiException) {

                    }
                })

        MySharedPreferences.clearAll()
        var intent = Intent(view.context, LoginActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        view.context.startActivity(intent)
        MyToast("退出成功")


    }
}